package com.luhuiguo.cms.server.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import com.luhuiguo.cms.server.service.ChannelService;
import com.luhuiguo.cms.server.util.MapperUtils;
import com.luhuiguo.cms.shared.model.ChannelModel;
import com.luhuiguo.cms.shared.rpc.GetChannel;
import com.luhuiguo.cms.shared.rpc.GetChannelResult;
import com.luhuiguo.gwt.dispatcher.server.ActionHandler;
import com.luhuiguo.gwt.dispatcher.server.ExecutionContext;
import com.luhuiguo.gwt.dispatcher.shared.exception.ActionException;

@Component
public class GetChannelHandler implements ActionHandler<GetChannel, GetChannelResult> {

    @Autowired
    private ChannelService channelService;

    @Override
    @PreAuthorize("isAuthenticated()")
    public GetChannelResult execute(GetChannel action, ExecutionContext context) throws ActionException {
        ChannelModel model = MapperUtils.map(channelService.getChannel(action.get()), ChannelModel.class);
        return new GetChannelResult(model);
    }

    @Override
    public Class<GetChannel> getActionType() {
        return GetChannel.class;
    }

    @Override
    public void rollback(GetChannel action, GetChannelResult result, ExecutionContext context) throws ActionException {

    }

}
